package com.itheima.mq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.Date;

/**
 * @author ChenYong
 * @data 2021/3/27 21:43
 */
@Component
public class PayConsumer {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderSettingService orderSettingService;

    //如果监听到就赋值给id
    @RabbitListener(queues = "dlx_queue")
    public void recv(Long orderId) {
        Order order = orderService.getById(orderId);
        Date orderDate = order.getOrderDate();
        if (order.getOrderStatus() == 0) {
            //未支付，回滚
            String date = DateUtil.formatDate(orderDate);
            OrderSetting orderSetting = orderSettingService.findByDate(date);
            orderSetting.setReservations(orderSetting.getReservations() - 1);
            orderSettingService.updateById(orderSetting);
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.lambda().ne(Order::getOrderStatus,1);
            orderService.remove(wrapper);
        }

    }
}
